package com.itheima.mall.controller.pmsController;

import com.itheima.mall.commons.R;
import com.itheima.mall.service.pmsService.PmsProductAttributeCategoryService;
import com.itheima.pojo.pms.PmsProductAttributeCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {
    @Autowired
    private PmsProductAttributeCategoryService productAttributeCategoryService;


    //添加商品属性分类
    @PostMapping("/create")
    public R addCreate(String name) {
        PmsProductAttributeCategory category = new PmsProductAttributeCategory();
        category.setName(name);
        productAttributeCategoryService.save(category);
        return R.ok("添加成功");

    }

    //商品类型删除
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Long id) {
        productAttributeCategoryService.removeById(id);
        return R.ok("删除成功");
    }

    //商品类型分页显示
    @GetMapping("/list")
    public R list(int pageNum, int pageSize) {
        Map map = productAttributeCategoryService.page(pageNum, pageSize);
        return R.ok(map);
    }


    //获取所有商品属性分类及其下属性
    @GetMapping("/list/withAttr")
    public R listWithAttr() {
        List<PmsProductAttributeCategory> categoryList = productAttributeCategoryService.list();
        return R.ok(categoryList);
    }

    //修改商品属性分类
    @PostMapping("/update/{id}")
    public R update(@PathVariable("id") Long id,String name){
        PmsProductAttributeCategory category= productAttributeCategoryService.updateById(id,name);
        return R.ok(1);
    }

    //获取单个商品属性分类信息
    @GetMapping("/{id}")
    public R oneList(@PathVariable("id") String id){
        PmsProductAttributeCategory category = productAttributeCategoryService.getById(id);
        return R.ok(category);
    }

}
